package com.oromwin.repos.client;

import java.io.File;
import java.lang.reflect.InvocationTargetException;

import org.apache.commons.logging.Log;

import com.oromwin.repos.common.Logger;
public class SimpleReposClient {
    private static Log log = Logger.getLog(SimpleReposClient.class);
    private static final String Volume = "Volume";
    private static final String Media = "Media";
    private static final String File = "File";
    private static final String create = "create";
    private static final String delete = "delete";
    private static final String save = "save";
    @SuppressWarnings("unused")
    private static final String read = "read";
    private static final ReposSocketClient client = new ReposSocketClient();
    
    public static void printUsage(String params) {
        System.out.format("java %s [ %s | %s | %s ] [ %s | %s | %s ] [ %s ]", 
                SimpleReposClient.class.getName(), Volume, Media, File,
                create, delete, save, params);
    }
    public static void main(String[] args) {
        if (args.length < 3) {
            printUsage("parameters");
            System.exit(0);
        }
        String target = args[0];
        String command = args[1];
        if (log.isDebugEnabled()) printUsage("parameters");
        try {
            if (target.equalsIgnoreCase(Volume)) {
                if (args.length == 3) {
                    String arg1 = args[2];
                    if (command.equalsIgnoreCase(create)) {
                        client.createVolume(arg1);
                    } else if (command.equalsIgnoreCase(delete)) {
                        client.deleteVolume(arg1);
                    } else {
                        SimpleReposClient.printUsage("VolumeName");
                    }
                } else {
                    printUsage("parameters");
                }
            } else if (target.equalsIgnoreCase(Media)) {
                if (command.equalsIgnoreCase(create)) {
                    if (args.length == 5) {
                        String arg1 = args[2];
                        File arg2 = new File(args[3]);
                        Long arg3 = Long.parseLong(args[4]);
                        client.createMedia(arg1, arg2, arg3);
                    } else {
                        SimpleReposClient.printUsage("VolumeName MediaPath MediaMaxBytes");
                    }
                } else if (command.equalsIgnoreCase(delete)) {
                    if (args.length == 4) {
                        String arg1 = args[2];
                        File arg2 = new File(args[3]);
                        client.deleteMedia(arg1, arg2);
                    } else {
                        SimpleReposClient.printUsage("VolumeName MediaPath");
                    }
                } else {
                    printUsage("parameters");
                }
            } else if (target.equalsIgnoreCase(File)) {
                if (command.equalsIgnoreCase(create)) {
                    if (args.length == 4) {
                        String arg1 = args[2];
                        File arg2 = new File(args[3]);
                        String fileId = client.createFile(arg1, arg2);
                        System.out.format("Retrieved fileId is {%s}", fileId);
                    } else {
                        SimpleReposClient.printUsage("VolumeName FilePath");
                    }
                } else if (command.equalsIgnoreCase(delete)) {
                    if (args.length == 4) {
                        String arg1 = args[2];
                        String arg2 = args[3];
                        client.deleteFile(arg1, arg2);
                    } else {
                        SimpleReposClient.printUsage("VolumeName FileId");
                    }
                } else if (command.equalsIgnoreCase(save)) {
                    if (args.length == 5) {
                        String arg1 = args[2];
                        String arg2 = args[3];
                        File arg3 = new File(args[4]);
                        client.saveFile(arg1, arg2, arg3);
                    } else {
                        SimpleReposClient.printUsage("VolumeName FileId LocalFilePath");
                    }
                } else {
                    printUsage("parameters");
                }
            } else {
                printUsage("parameters");
            }
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        System.exit(0);
    }
}
